import axios from 'axios'
//import { ElMessage } from 'element-plus'
import { toast } from '~/composables/util'
//import { useCookies } from '@vueuse/integrations/useCookies'
import { getToken } from '~/composables/auth'
import store from './store'


const service = axios.create({
  baseURL: "/api"
})

// 添加请求拦截器
service.interceptors.request.use(function (config) {
  // 向header头自动添加token
  //const cookie = useCookies()
  //const token = cookie.get("admin-token")
  const token = getToken()
  
  if(token){
    config.headers["token"] = token
  }

  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
service.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response.data.data;
}, function (error) {
  // 对响应错误做点什么
  const msg = error.response.data.msg || "请求失败"

  if(msg=="非法token，请先登录！"){
    store.dispatch('logout').finally(()=>location.reload())
  }

  toast( msg,'error')
  /* ElMessage({
    message: error.response.data.msg || "请求失败",
    type: 'error',
    plain: true,
    duration:3000,
  }) */

  return Promise.reject(error);
});
 
export default service
